<?php
/**
 * Created by PhpStorm.
 * User: yangfan
 * Date: 2019/11/23
 * Time: 3:45 PM
 */

namespace App\Entities;

class DistributorEntity extends ABaseEntity
{
    public function __construct(DistributorModel $model)
    {
        $this->model = $model;
    }

    /**
     * @param int $userId
     * @return DistributorEntity/null
     */
    public static function findByUserId(int $userId)
    {
        $distributor = DistributorModel::where('dis_id', '=', $userId)->first();
        if (is_null($distributor)) {
            return null;
        }
        return new DistributorEntity($distributor);
    }

    /**
     * @param int $userId
     * @return DistributorEntity
     */
    public static function findOrCreateByUserId(int $userId)
    {
        $distributorEntity = self::findByUserId($userId);
        if (!is_null($distributorEntity)) {
            return $distributorEntity;
        }

        $user = UserEntity::find($userId);
        $newInstance = self::getNewInstance();
        $newInstance->setDisId($user->getUserId());
        $newInstance->setDisName($user->getName());
        $newInstance->setPhone($user->getPhone());
        $newInstance->save();

        return $newInstance;
    }

    /**
     * @return int
     */
    public function getId()
    {
        return (int)$this->model->id;
    }

    /**
     * 分销商ID
     * @return int
     */
    public function getDisId()
    {
        return $this->model->dis_id;
    }

    /**
     * 分销商ID
     * @param int $disId
     */
    public function setDisId(int $disId)
    {
        $this->model->dis_id = $disId;
    }

    /**
     * 分销商 name
     * @return string
     */
    public function getDisName()
    {
        return $this->model->dis_name;
    }

    /**
     * 分销商ID
     * @param string $disName
     */
    public function setDisName(string $disName)
    {
        $this->model->dis_name = $disName;
    }

    /**
     * 分销商 phone
     * @return string
     */
    public function getPhone()
    {
        return $this->model->phone;
    }

    /**
     * 分销商 phone
     * @param string $phone
     */
    public function setPhone(string $phone)
    {
        $this->model->phone = $phone;
    }

    /**
     * @return string
     */
    public function getFreezeBalance()
    {
        return $this->model->freeze_balance ?: '0';
    }

    /**
     * @param string $freezeBalance
     */
    public function setFreezeBalance(string $freezeBalance)
    {
        $this->model->freeze_balance = $freezeBalance;
    }

    /**
     * @return string
     */
    public function getPreBalance()
    {
        return $this->model->pre_balance;
    }

    /**
     * @param string $preBalance
     */
    public function setPreBalance(string $preBalance)
    {
        $this->model->pre_balance = $preBalance;
    }


    /**
     * @return string
     */
    public function getBalance()
    {
        return $this->model->balance;
    }

    /**
     * @param string $balance
     */
    public function setBalance(string $balance)
    {
        $this->model->balance = $balance;
    }
}
